package edu.cs346.group5.logic;

import java.sql.Date;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import edu.cs346.group5.backend.Database;
import edu.cs346.group5.datastores.Comment;
import edu.cs346.group5.datastores.Topic;

public class ViewTopic {
	
	public static Topic GetTopic(int itemId)
	{
		List<HashMap<String, Object>> results = null;
		
		try {
			results = Database.select("Select * From Items i INNER JOIN Posts p ON i.id = p.item_id Where i.id = '" + itemId + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Topic topic = new Topic();
		topic.setCategoryId((int) results.get(0).get("category"));
		topic.setDescription(results.get(0).get("description").toString());
		topic.setImageUrl(results.get(0).get("image").toString());
		topic.setReferenceURL(results.get(0).get("reference_url").toString());
		topic.setTopicName(results.get(0).get("content").toString());
		topic.setRating(results.get(0).get("rating") == null ? 0 : (float)results.get(0).get("rating"));
		topic.setPostId((int)results.get(0).get("id"));
		topic.setTotalPoints((int)results.get(0).get("total_points"));
		topic.setNumberOfVotes((int)results.get(0).get("number_of_votes"));
		topic.setItemId((int)results.get(0).get("item_id"));
		return topic;
	}
	
	public static Comment[] GetTopicComments(int itemId){
		List<HashMap<String, Object>> results = null;
		try {
			results = Database.select("Select * From Posts p INNER JOIN Users u ON u.id = posted_by Where in_reply_to = '" + itemId + "' Order By posted_date Desc");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Comment[] comments = new Comment[results.size()];
		Comment temp;
		for(int i = 0; i < comments.length; i++){
			temp = new Comment();
			temp.setContent(results.get(i).get("content").toString());
			temp.setId((int)results.get(i).get("id"));
			temp.setPostDate((Date) results.get(i).get("posted_date"));
			temp.setPostedBy(results.get(i).get("first_name").toString() + " " + results.get(i).get("last_name").toString());
			comments[i] = temp;
		}
		return comments;
	}
	
	public static void InsertComment(Comment comment){
		try {
			Database.insert("Posts", "posted_date, content, in_reply_to, posted_by", "'" + comment.getPostDate() + "', " + "'" + comment.getContent() + "', " + "'" + comment.getInReplyTo() + "', " + "'" + comment.getPostedById() + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void InsertRating(int rating, int postId){
		try {
			Database.execRaw("Update Posts Set total_points = total_points +'" + rating + "' , number_of_votes = number_of_votes + 1 Where id = '" + postId + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void DeleteTopic(String content, Date date){
		try {
			Database.execRaw("Delete From Posts Where posted_date = '" + date + "' AND content = '" + content + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void DeleteTopic(int itemId, int postId){
		try {
			Database.execRaw("Delete From Posts Where id = '" + postId + "' OR in_reply_to = '" + itemId + "'");
			Database.execRaw("Delete From Items Where id = '" + itemId + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void AddToWishList(int userId, int itemId){
		List<HashMap<String, Object>> results = null;
		try {
			results = Database.select("select * from Wishlists where user_id = '" + userId + "' AND item_id = '" + itemId + "'");
			if(results.size() == 0){
				Database.execRaw("Insert into Wishlists(user_id,  item_id) values('" + userId + "', " + "'" + itemId + "')");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
